package com.google.project.client.ProjectManager;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.project.client.Service.ProjTaskService;
import com.google.project.client.Service.ProjTaskServiceAsync;
import com.google.project.client.Service.ProjectStaffService;
import com.google.project.client.Service.ProjectStaffServiceAsync;
import com.google.project.shared.ProjectDTO;
import com.google.project.shared.ProjectStaffDTO;
import com.google.project.shared.ProjectTaskDTO;

public class AssignTaskToOptDialog extends DialogBox {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField ListBox cboOpt;
	@UiField Button btnClose;
	@UiField Button btnAdd;


	interface Binder extends UiBinder<Widget, AssignTaskToOptDialog> {
	}
	private final ProjectStaffServiceAsync projStaffSv = GWT.create(ProjectStaffService.class);
	private final ProjTaskServiceAsync taskSv = GWT.create(ProjTaskService.class);
	private  List<ProjectTaskDTO> listTask=new ArrayList<ProjectTaskDTO>();
	public AssignTaskToOptDialog() {
		setWidget(binder.createAndBindUi(this));
	}
	ProjectDTO currProject=new ProjectDTO();
	public AssignTaskToOptDialog(String projectId) {
		setWidget(binder.createAndBindUi(this));
		
	}
	public AssignTaskToOptDialog(ProjectDTO currProject, List<ProjectTaskDTO> listTask) {
		this.listTask=listTask;
		this.currProject=currProject;
		setWidget(binder.createAndBindUi(this));
		projStaffSv.getProjectStaffList(currProject.getProjectID(), new AsyncCallback<List<ProjectStaffDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
				
			}

			@Override
			public void onSuccess(List<ProjectStaffDTO> result) {
				for (ProjectStaffDTO projectStaffDTO : result) {
					if(projectStaffDTO.getPosition()==3 && (projectStaffDTO.getStatus()==1 ||projectStaffDTO.getStatus()==0 )){
						String display=projectStaffDTO.getUserName()+" ("+projectStaffDTO.getFullName()+")";
						String value=projectStaffDTO.getUserId();
						cboOpt.addItem(display, value);
					}
				}
				
			}
		});
	}
	void close()
	{
		this.hide();
	
	}
	@UiHandler("btnClose")
	void onBtnCloseClick(ClickEvent event) {
		close();
		//PMEntrypoint.get().PMProjectDetail(listTask.get(0).getProjectId());
	}
	@UiHandler("btnAdd")
	void onButtonClick(ClickEvent event) {
		
		if(cboOpt.getValue(cboOpt.getSelectedIndex()).equals("-1"))
		{
			Window.alert("Bạn phải chọn công việc để giao");
		}
		else
		{	
			String nameOpt=cboOpt.getItemText(cboOpt.getSelectedIndex());
			String []name=nameOpt.split(" ");
			
			for (ProjectTaskDTO projectTaskDTO : listTask) {
				projectTaskDTO.setOptUserId(cboOpt.getValue(cboOpt.getSelectedIndex()));
				projectTaskDTO.setOptAccountName(name[0]);
				taskSv.updateTask(projectTaskDTO, new AsyncCallback<Void>() {
					
					@Override
					public void onSuccess(Void result) {
						closeDialog();
						
						
					}
					
					@Override
					public void onFailure(Throwable caught) {
						
					}
				});
			}
			Window.alert("Giao việc thành công");
			
		}
	}
	public void closeDialog(){
		this.hide();
		//PMEntrypoint.get().PMAssignTask(currProject.getProjectID());
	}
}
